package com.wencst.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {

	  @PostMapping("/user/login")
	  public String login(HttpServletRequest request, HttpServletResponse response ,@RequestParam String username, @RequestParam String password, Model model) {
		  System.out.println("-----post page----- userlogin "+request.getRemoteUser());
		  if(StringUtils.isEmpty(username)|| StringUtils.isEmpty(password)) {
			  return "error/500";
		  }
		  if("admin".equalsIgnoreCase(username)) {
			  model.addAttribute("title", "access/home");
			  model.addAttribute("message", "i am here!");
			  model.addAttribute("date", new Date());
			  return "accessauth/home";
//			  return "index";
		  }
		  return "redirect:/users";
	  }
	  @GetMapping("/user/login")
	  public String login1(HttpServletRequest request, HttpServletResponse response ,@RequestParam String username, @RequestParam String password, Model model) {
		  System.out.println("-----get page----- userlogin "+request.getRemoteUser());
		  if(StringUtils.isEmpty(username)|| StringUtils.isEmpty(password)) {
			  return "error/500";
		  }
		  if("admin".equalsIgnoreCase(username)) {
			  model.addAttribute("title", "access/home");
			  model.addAttribute("message", "i am here!");
			  model.addAttribute("date", new Date());
			  return "accessauth/home";
		  }
		  return "error/500";
	  }
}
